/*
 * Timer.h
 *
 *  Created on: 6-mrt-2012
 *      Author: Bart
 */

#ifndef TIMER_H_
#define TIMER_H_

#include "xparameters.h"
#include "xio.h"
#include "xtmrctr.h"
#include "xintc.h"
#include "xil_exception.h"

#define TMRCTR_DEVICE_ID        XPAR_TMRCTR_0_DEVICE_ID
#define INTC_DEVICE_ID          XPAR_XPS_INTC_0_XPS_TIMER_0_INTERRUPT_INTR
//#define TMRCTR_INTERRUPT_ID     XPAR_INTC_0_TMRCTR_0_VEC_ID

/*
 * One timer is assigned for the main timer
 */
#define TIMER_CNTR_0     0


//TODO zorg dat de compiler dat het timing interval kan gekozen worden en de compiler de rest uitrekent.
#define PLB_CLOCK_PERIOD        (1.0/125000000.0)
#define PLB_CLOCK_FREQ          (125000000)
#define TIMING_INTERVAL         (1)     //in ms
#define MAX_COUNT                       (0hFFFFFFFF)
// Down counter
//#define TIMING_INTERVAL       ((RESET_VALUE + 2) * PLB_CLOCK_PERIOD)
#define RESET_VALUE             ((TIMING_INTERVAL * (PLB_CLOCK_FREQ / 1000)) - 2)

/*
// Up counter
//#define TIMING_INTERVAL       ((MAX_COUNT - RESET_VALUE + 2) * PLB_CLOCK_PERIOD)
#define RESET_VALUE             (MAX_COUNT + 2 - (TIMING_INTERVAL / PLB_CLOCK_PERIOD))
*/

/*
 * C -> De compiler herkent dit als C-code
 */
#ifdef __cplusplus
extern "C"{
        /*
         * Initialize and starts the timer.
         */
        int initialize();

        /*
         * Returns the number of microseconds since the timer is initialized.
         * This number will overflow (go back to zero), after approximately 70 minutes.
        */
        Xuint32 micros(void);

        /*
         * Returns the number of milliseconds since the timer is initialized.
         * This number overflows (go back to zero), after approximately 50 days.
         */
        Xuint32 millis(void);

        /*
         * Pauses the program for the amount of time (in miliseconds) specified as parameter.
         *
         * Caveat: All other functions (except interrupts) will be stopped for the duration of the delay.
         */
        void delay(Xuint32);
        //TODO
        /*
         * Deze functies kunnen in een aparte header file:
         * "microblaze_private.h" --> Een aparte header file voor
         */

#endif
#ifdef __cplusplus
}
#endif

///*
// * C++
// * Onderstaande code is C++, dit is voornamelijk een probeersel
// */
//#ifdef __cplusplus
//class Timer{
//
//private:
//      //TODO mss beter define gebruiken -> owv compiler ipv runtime
//      /*
//      Xuint16 DeviceId = XPAR_XPS_TIMER_0_DEVICE_ID;
//      Xuint32 DeviceBaseAddress = XPAR_XPS_TIMER_0_BASEADDR;
//      Xuint32 InterruptId = XPAR_INTC_0_TMRCTR_0_VEC_ID;
//      */
//
//      /*
//       * Instance of TimerCounter
//       */
//      XTmrCtr TimerCounterInst;
//      /*
//       * Instance of InterruptController
//       */
//      XIntc InterruptController;
//
//      //TODO public of private?
//      /*
//       * The following variables are shared between non-interrupt processing and
//       * interrupt processing such that they must be global.
//       */
//      volatile int TimerExpired;
//
//      //void TimerCounterHandler(void *CallBackRef, Xuint8 TmrCtrNumber);
//
//
//
//public:
//      /*
//       * Initialize and starts the timer.
//       */
//      int initialize();
//
//      /*
//       * Returns the number of microseconds since the timer is initialized.
//       * This number will overflow (go back to zero), after approximately 70 minutes.
//       *
//       * micros() is updated when this function is called. The value of the timer counter will be read
//      */
//      Xuint32 micros();
//
//      /*
//       * Returns the number of milliseconds since the timer is initialized.
//       * This number overflows (go back to zero), after approximately 50 days.
//       *
//       * Millis is incremented every 1ms by an interrupt.
//       */
//      Xuint32 millis();
//
//      static int setupIntrSystem(XIntc* IntcInstancePtr,
//                                                       XTmrCtr* TmrCtrInstancePtr,
//                                                       Xuint16 DeviceId,
//                                                       Xuint16 IntrId,
//                                                       Xuint8 TmrCtrNumber);
//
//};
//#endif

#endif /* TIMER_H_ */

